package com.zmz.DateTest;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Create with IntelliT IDEA
 *
 * @Author: zhengmingzhe
 * @Date: 2022/11/03/20:48
 * @Description: 定时任务每月8号，18号，28号那天报送调用下面的方法获取8号，18号，28号的区间 比如上个月28号到这个月8号，这个月8号到18号，18号到28号（用于报送需求）
 */
public class DateSection {
    public static void main(String[] args) throws ParseException {
//        getMonthFullDay();
//        format();
        //获取上个季度
        getLastQuarter();
    }

    /**
     * 获取指定的日期区间
     *
     * @param
     * @return
     */
    public static List<String> getMonthFullDay() {
        List<String> dateSection = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //格式化日期
//        String currntDate = sdf.format(new Date());
        //下面定时任务的日期替代 ，定时任务的日期是每个月的8号，18号，28号
        String currentDate = "2022-01-18 16:56:";
        int year = Integer.parseInt(currentDate.substring(0, 4));
        int month = Integer.parseInt(currentDate.substring(5, 7));
        int currentDay = Integer.parseInt(currentDate.substring(8, 10));
        Calendar cal = Calendar.getInstance();// 获得当前日期对象
        cal.clear();// 清除信息
        if (currentDay == 8) {
            int day = 28;// 所有月份从1号开始，这里可以设置上个月指定的日期
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month - 2);// 1月从0开始
            cal.set(Calendar.DAY_OF_MONTH, day);
            //拿到上个月28号,存入list
            String startDate = sdf.format(cal.getTime());
            currentDate = currentDate.substring(0, 10);
            dateSection.add(startDate);
            dateSection.add(currentDate);
            dateSection.stream().forEach(n -> System.out.println(n));
            return dateSection;
        } else {
            int day = Integer.parseInt(currentDate.substring(8, 10));
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month - 1);// 1月从0开始
            cal.set(Calendar.DAY_OF_MONTH, day);
            cal.add(Calendar.DAY_OF_MONTH, -10);
            String startDate = sdf.format(cal.getTime());
            currentDate = currentDate.substring(0, 10);
            dateSection.add(startDate);
            dateSection.add(currentDate);
            dateSection.stream().forEach(n -> System.out.println(n));
            return dateSection;
        }
    }

    /*
     * @Description  将日期字符串格式"yyyy-MM-dd"格式化为"yyyyMMdd"
     * @Param
     * @return
     **/
    public static void format() throws ParseException {
        String indate = "2021-07-20";
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        //将indate转化为Date类型
        Date date = df2.parse(indate);
        System.out.println(date);
        String formatDate = df.format(df2.parse(indate));
        System.out.println(formatDate);

    }

    /*
     * @Description  获取上个季度 和上个季度最后一天
     * @Param
     * @return
     **/
    public static void getLastQuarter() {
        //获取当前年份和季度
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM");
        String format = sdf.format(new Date());
        //年份
        String year = format.substring(0, 4);
        //转整形方便计算
        int intyear = Integer.parseInt(year);
        //当前月份
        String month = format.substring(5, 7);
        int intmonth = Integer.parseInt(month);
        //当前季度（整形方便计算）
        int quarter = (intmonth % 3 == 0) ? (intmonth / 3) : (intmonth / 3 + 1);
        //获取上个季度
        if (quarter > 1) {
            quarter -= 1;
        } else {
            quarter = 4;
            intyear -= 1;
        }
        //上个季度的年份和季度
        System.out.println(intyear);
        System.out.println(quarter);
        //获取上个季度的倒数第二个月
        int lastMonth = quarter * 3 - 1;
        //获取上季度最后一天
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, intyear);
        cal.set(Calendar.MONTH, lastMonth);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String lastDayOfMonth = sdf.format(cal.getTime());
        System.out.println(lastMonth);
        System.out.println(lastDayOfMonth);
    }
}

























